package com.verisette.ncos.web.accesscontrol.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;

import org.corp.agi.service.bean.ServiceRequest;
import org.corp.agi.service.bean.ServiceResponse;
import org.corp.agi.service.bean.UserBean;
import org.corp.agi.web.action.BaseAction;
import org.corp.agi.web.constant.WebConstant;
import org.corp.agi.web.util.ServiceRequestFactory;
import com.verisette.ncos.common.ProjectConstant;
import com.verisette.ncos.persistence.domain.MasterUserProfile;
import com.verisette.ncos.service.accesscontrol.gateway.CustomAccessControlService;

/**
 * {Description here}
 *
 * @author Thippawan Muangchu (Tip)
 * @since Oct 4, 2010 3:32:25 PM
 * @version 1.0.0
 * @see 
 */
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class ChangePasswordAction extends BaseAction {
	private Logger logger = Logger.getLogger(ChangePasswordAction.class);

	// input from page
	private String oldPassword;
	private String newPassword;
	private String confirmPassword;
	
	protected String username;
	protected String password;
	protected String role;
	
	private  CustomAccessControlService customAccessControlService;
	@Autowired
	@Required
	public void setCustomAccessControlService(CustomAccessControlService customAccessControlService) {
		this.customAccessControlService = customAccessControlService;
	}

	private void clearSessionMessage(){
		httpServletRequest.getSession().removeAttribute("SESSION_MESSAGE_KEY");
	}
	
	public String init() throws Exception {
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": init" );
		clearSessionMessage();
		return ActionSupport.SUCCESS;	
	}
	
	public String changePassword() throws Exception {
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": changePassword" );
		clearSessionMessage();
		
		if(httpServletRequest.getSession().getAttribute(WebConstant.SESSION_USERBEAN_KEY) != null){
			
			UserBean userBean = (UserBean) httpServletRequest.getSession().getAttribute(WebConstant.SESSION_USERBEAN_KEY);
			
			//create userBean and save to session
			MasterUserProfile masUser = new MasterUserProfile();
			ServiceRequest<Map<String, Object>> request = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);
			
			masUser.setUsername(userBean.getUsername());
			masUser.setPassword(userBean.getPassword());
			masUser.setRole(userBean.getRole());

			Map<String, Object> chgPwdInputMap = new HashMap<String, Object>();
			
			chgPwdInputMap.put(ProjectConstant.USER_PROFILE, masUser);
			chgPwdInputMap.put(ProjectConstant.USER_PROFILE_IP_ADDRESS, httpServletRequest.getRemoteAddr());
			chgPwdInputMap.put(ProjectConstant.CHANGE_PASSWORD_OLD_PASSWORD, this.oldPassword);
			chgPwdInputMap.put(ProjectConstant.CHANGE_PASSWORD_NEW_PASSWORD, this.newPassword);
			chgPwdInputMap.put(ProjectConstant.CHANGE_PASSWORD_CONFIRM_PASSWORD, this.confirmPassword);
			
			request.setValueObject(chgPwdInputMap);
			
			ServiceResponse<UserBean> response = customAccessControlService.changePassword(request);

			if(isServiceResponseSuccess(response))
			{
				
				UserBean loginUser = response.getValueObject();	
				httpServletRequest.getSession().setAttribute(WebConstant.SESSION_USERBEAN_KEY, response.getValueObject());
				
			}
			else
			{
				return ActionSupport.SUCCESS;
			}
			
		}
		return "logout";
	}

	// Getter-Setter
	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}
	
	
}
